package com.sunni.service.impl;

import com.sunni.entity.Book;
import com.sunni.entity.Bookcase;
import com.sunni.entity.Borrow;
import com.sunni.repository.BookRepository;
import com.sunni.repository.BorrowRepository;
import com.sunni.repository.impl.BookRepositoryImpl;
import com.sunni.repository.impl.BorrowRepositoryImpl;
import com.sunni.service.BookService;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class BookServiceImpl implements BookService {
    private BookRepository bookRepository = new BookRepositoryImpl();
    private final Integer LIMIT = 6;

    private BorrowRepository borrowRepository = new BorrowRepositoryImpl();

    @Override
    public List<Book> findAll(Integer page) {
        Integer index = (page-1)*LIMIT;
        return bookRepository.findAll(index,LIMIT);
    }

    @Override
    public Integer getPageTotal() {
        int projectTotal=bookRepository.getProjectTotal();
        int pageTotal = (int)Math.ceil( (double) projectTotal/ LIMIT);
        return  pageTotal;
    }

    /**
     * 点击借阅，添加借阅信息 到 borrow表
     * @param bookId 借的书的 id
     * @param readId 借书人的 id
     * @return
     */
    @Override
    public boolean addBorrow(Integer bookId, Integer readId) {

        //借书时间
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String borrowTime = simpleDateFormat.format(date);
        //还书时间，借书时间+14天
        Calendar calendar = Calendar.getInstance();
        int dates = calendar.get(Calendar.DAY_OF_YEAR) + 14;
        calendar.set(Calendar.DAY_OF_YEAR, dates);
        Date date2 = calendar.getTime();
        String returnTime = simpleDateFormat.format(date2);

        Boolean isScucess = borrowRepository.insertBorrow(bookId,readId,borrowTime,returnTime,null,0)>0 ? true:false;

        return isScucess;
    }

    @Override
    public List<Borrow> findAllByReaderid(Integer readerId,Integer page) {
        Integer index = (page-1)*LIMIT;
        return borrowRepository.findAllByReaderid(readerId,index,LIMIT);
    }

    @Override
    public Integer getPageCountBorrowByReaderid(Integer readerId) {
        int countBorrowProject = borrowRepository.countBorrowByReaderid(readerId);
        int countBprrowPage = (int)Math.ceil( (double) countBorrowProject/ LIMIT);
        return countBprrowPage;
    }

    @Override
    public Integer getPageCountBorrowByState0(Integer state) {
        int countByBorrowState0  = borrowRepository.countBorrowByState0(state);
        return (int)Math.ceil( (double) countByBorrowState0/ LIMIT);
    }

    @Override
    public List<Borrow> findAllByState0(Integer state0, Integer page) {
        int index = (page-1)*LIMIT;
        return borrowRepository.findAllByState0(0,index,LIMIT);
    }

    @Override
    public Integer handleByBorrowid(Integer borrowId, Integer state, Integer adminId) {
        return borrowRepository.handleByBorrowid(borrowId,state,adminId);
    }
}
